package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.JxStudent;
import com.ruoyi.system.domain.JxStudentStatusLog;
import com.ruoyi.system.mapper.JxStudentMapper;
import com.ruoyi.system.mapper.JxStudentStatusLogMapper;
import com.ruoyi.system.service.IJxStudentService;

@Service
public class JxStudentServiceImpl implements IJxStudentService {
    @Autowired
    private JxStudentMapper jxStudentMapper;

    @Autowired
    private JxStudentStatusLogMapper statusLogMapper;

    @Override
    public JxStudent selectJxStudentById(Long studentId) {
        return jxStudentMapper.selectJxStudentById(studentId);
    }

    @Override
    public List<JxStudent> selectJxStudentList(JxStudent query) {
        return jxStudentMapper.selectJxStudentList(query);
    }

    @Override
    public int insertJxStudent(JxStudent entity) {
        entity.setCreateBy(SecurityUtils.getUsername());
        return jxStudentMapper.insertJxStudent(entity);
    }

    @Override
    public int updateJxStudent(JxStudent entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        return jxStudentMapper.updateJxStudent(entity);
    }

    @Override
    public int deleteJxStudentByIds(Long[] studentIds) {
        return jxStudentMapper.deleteJxStudentByIds(studentIds);
    }

    @Override
    public int deleteJxStudentById(Long studentId) {
        return jxStudentMapper.deleteJxStudentById(studentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeStatus(Long studentId, String toStatus, String operator, String remark) {
        JxStudent old = jxStudentMapper.selectJxStudentById(studentId);
        if (old == null) {
            return 0;
        }
        String fromStatus = old.getStatus();
        old.setStatus(toStatus);
        old.setUpdateBy(operator);
        int rows = jxStudentMapper.updateJxStudent(old);

        JxStudentStatusLog log = new JxStudentStatusLog();
        log.setStudentId(studentId);
        log.setFromStatus(fromStatus);
        log.setToStatus(toStatus);
        log.setOperator(operator);
        log.setCreateBy(operator);
        log.setRemark(remark);
        statusLogMapper.insertJxStudentStatusLog(log);

        return rows;
    }
}


